<template>
  <div>
    <el-row :gutter="20"
    
    >
      <el-col :span="8"
      >
        <el-input v-model="search.name" placeholder="请输入要查询的部门名"></el-input>
      </el-col>
      <el-button @click="dosearch()" type="primary" icon="el-icon-search">搜索</el-button>
      <el-button type="primary" icon="el-icon-circle-plus-outline" @click="add(0)">添加部门</el-button>
      <el-button type="danger" icon="el-icon-delete" @click="batchdel()">批量删除</el-button>
    </el-row>
    <br>
    <el-table 
    :data="tableData" 
    style="width: 100%" 
    row-key="id"
    @selection-change="handleSelectionChange">
      <el-table-column label="id" width="50" type="selection"></el-table-column>
      <el-table-column label="id" width="100" prop="id"></el-table-column>
      <el-table-column label="部门名称" width="180" prop="name"></el-table-column>
      <el-table-column label="办公地址" width="180" prop="address"></el-table-column>
      <el-table-column label="负责人" width="100" prop="leader"></el-table-column>
      <el-table-column label="部门类型" width="180" prop="">
        <template slot-scope="scope">
          <span v-if="scope.row.type === 1">医疗科室</span>
          <span v-else>非医疗科室</span>
        </template>
      </el-table-column>
      <el-table-column label="是否有效" width="100" prop="">
        <template slot-scope="scope">
          <span v-if="scope.row.active === 1">有效</span>
          <span v-else>无效</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <template v-if="scope.row.active">
            <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
          <template v-else>
            <el-button size="mini" type="success" @click="handleBack(scope.row.id)">恢复</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top:20px"
      background
      layout="prev, pager, next"
      :page-count="pages"
      :current-page.sync="query.pageNo"
      :page-size="query.pageSize"
      @current-change="getData"
    ></el-pagination>
    <el-dialog :title="title" :visible.sync="show" :close-on-click-modal="false">
      <DepartmentEdit 
      v-if="show" 
      :show.sync="show" 
      :editid="editid"
      @getdata="getData()">
      </DepartmentEdit>
    </el-dialog>
  </div>
</template>

<script>
import DepartmentEdit from '@/views/department/edit'
export default {
    name: 'Department',
    data(){
        return {
            tableData: [],
            query:{
                name: '',
                pageSize: 10,
                pageNo: 1,
            },
            search:{
                name: ''
            },
            pages: 0,
            show: false,
            editid : null,
            multipleSelection: [],
            title: '',
            parentId: 0
        }
    },
    created() {
        this.getData()
    },
    methods: {
        getData(){
            this.axios.get('/department/list',response =>{
                this.tableData=response.obj.records
                this.pages=response.obj.pages
            },this.query)
        
        },
        dosearch(){
            this.query.name = this.search.name
            this.query.pageNo = 1
            this.getData()
        },
        handleEdit(id)
        {
            this.title='修改部门'
            this.editid = id
            this.show = true
        },
        add(id){
            this.parentId=id
            this.title='添加部门'
            this.editid = ''
            this.show = true
        },
        handleDelete(id) {
          this.axios.del('/department/del',response =>{
            this.getData()
          },{'id': id})
      },
      handleSelectionChange(val)
      {
        this.multipleSelection = val
      },
      batchdel()
      {
        if(this.multipleSelection.length===0)
        {
          this.$message("没有任何被选中的数据")
        }
        else{
          const ids=[]
          for(let i=0 ;i<this.multipleSelection.length;i++)
          {
            ids.push(this.multipleSelection[i].id)
          }
           this.axios.del('/department/batchdel',response =>{
            this.getData()
          },{'ids':ids})
        }
      },
      handleBack(id)
      {
        this.axios.del('/department/back',response =>{
            this.getData()
          },{'id': id},'back')
      }
    },
    components:{
        DepartmentEdit
    }
}

</script>

<style>
</style>